package com.xmall.order.service.impl.other;

import com.xmall.define.base.Result;
import com.xmall.define.dto.login.MemberDto;
import com.xmall.define.dto.member.DeliveryAddressDto;
import com.xmall.define.dto.product.SkuInfoDto;
import com.xmall.define.dto.product.SkuQuantity;
import com.xmall.order.service.other.IRemoteService;
import com.xmall.order.service.remote.MemberServiceFeign;
import com.xmall.order.service.remote.ProductServiceFeign;
import com.xmall.utils.XmallUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RemoteServiceImpl implements IRemoteService {

    @Resource
    private ProductServiceFeign productServiceFeign;

    @Resource
    private MemberServiceFeign memberServiceFeign;

    /**
     * 远程请求member服务获取收货地址信息
     */
    @Override
    public DeliveryAddressDto deliveryAddress(Long addressId) {
        Result<DeliveryAddressDto> result = memberServiceFeign.deliveryAddressInfo(addressId);
        XmallUtils.checkRemoteResult(result);
        return result.getData();
    }

    /**
     * 远程请求product服务获取sku列表信息
     */
    @Override
    public List<SkuInfoDto> skuInfoList(List<SkuQuantity> skuQuantityList) {
        Result<List<SkuInfoDto>> skuList = productServiceFeign.list(skuQuantityList.stream().map(skuQuantity -> skuQuantity.getSkuId()).collect(Collectors.toList()));
        XmallUtils.checkRemoteResult(skuList);
        return skuList.getData();
    }

    /**
     * 远程请求member服务获取用户信息
     */
    @Override
    public MemberDto memberInfo(Long memberId) {
        Result<MemberDto> memberResult = memberServiceFeign.memberInfo(memberId);
        XmallUtils.checkRemoteResult(memberResult);
        return memberResult.getData();
    }

}
